package com.waves.user.controller;


import com.waves.core.utils.AssertUtil;
import com.waves.core.utils.LoginUtils;
import com.waves.core.web.domain.R;
import com.waves.core.web.domain.TableDataInfo;
import com.waves.user.domain.dto.FollowPageDTO;
import com.waves.user.domain.dto.UserUpdateDTO;
import com.waves.user.domain.dto.user.*;
import com.waves.user.domain.enums.RoleEnum;
import com.waves.user.domain.vo.UserDetailVO;
import com.waves.user.domain.vo.UserInfoVO;
import com.waves.user.service.IRoleService;
import com.waves.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dlw
 * @since 2023-03-19
 */
@RestController
@RequestMapping("/userApi/user")
@Api(tags = "用户管理相关接口")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private IRoleService iRoleService;

    @GetMapping("/userInfo")
    @ApiOperation("用户详情")
    public R<UserInfoVO> getUserInfo() {
        return R.ok(userService.getUserInfo(LoginUtils.getUserId()));
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public R<List<com.waves.user.domain.dto.SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoDTO req) {
        return R.ok(userService.getSummeryUserInfo(req));
    }

    @PutMapping("/name")
    @ApiOperation("修改用户名")
    public R<Void> modifyName(@Valid @RequestBody ModifyNameDTO req) {
        userService.modifyName(req);
        return R.ok();
    }

    @PutMapping("/black")
    @ApiOperation("拉黑用户")
    public R<Void> black(@Valid @RequestBody BlackDTO req) {
        Long uid = LoginUtils.getUserId();
        boolean hasPower = iRoleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPower, "没有权限");
        userService.black(req);
        return R.ok();
    }

    /**
     * 关注和取消关注
     * @param uid 被关注的人id
     * @return
     */
    @ApiOperation(value = "关注/取消关注")
    @GetMapping("/followUser/{uid}")
    public R<Object> followUser(@PathVariable long uid) {
        return userService.followUser(uid);
    }

    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询某人关注列表或粉丝列表")
    @GetMapping("/pageFollowUser")
    public TableDataInfo<UserDetailVO> pageFollowUser(FollowPageDTO dto) {
        return userService.listFollowUser(dto);
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/getUserInfo/{uid}")
    @ApiOperation("查询用户信息")
    public R<UserDetailVO> getUserInfos(@PathVariable long uid) {
        return userService.getUserInfos(uid);
    }

    /**
     * 查询用户信息
     * @return
     */
    @PostMapping("/updateUserInfo")
    @ApiOperation("修改用户信息")
    public R<Object> updateUserInfo(@RequestBody UserUpdateDTO dto) {
        return userService.updateUserInfo(dto);
    }

}

